/*
 * @Author: swl
 * @Date: 2023-07-06 17:59:31
 * @LastEditors: swl
 * @LastEditTime: 2023-07-08 10:37:53
 */
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
  providedIn: 'root',
})
export class MusicplayService {
  isPlaying = false;
  playingMusic: any;
  durationTime: number = 0; // 总时间
  currentTime2: number = 0; // 当前时间
  progress: string = '';
  isRotating = false;
  constructor() {
    this.player = new Audio();
  }
  private progressSubject: Subject<number> = new Subject<number>();
  public progress$ = this.progressSubject.asObservable();
  player: HTMLAudioElement;
  playMusic(src: string) {
    this.player = new Audio(src);
    this.player.autoplay = true;
    this.player.play();
    this.player.addEventListener('timeupdate', () => {
      this.currentTime2 = this.player.currentTime;
      let progress = (
        (this.player.currentTime / this.player.duration) *
        100
      ).toFixed(2);
      this.progressSubject.next(Number(progress));
    });
    this.player.currentTime = this.currentTime2;
  }
  pauseMusic() {
    this.player.pause();
  }
}
